<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <tree-tools slot-scope="{ data }" :tree-node="data" @addDepts="addDepts" @editDepts="editDepts" @delDepts="getDepartments" />
        </el-tree>
      </el-card>
    </div>
    <!-- :show-dialog="showDialog" -->
    <!-- v-model="showDialog" -->
    <add-dept
      v-if="originalDeparts.length > 0"
      ref="addDeptRef"
      :show-dialog.sync="showDialog"
      :depart-list="originalDeparts"
      :tree-node="treeNode"
      @addNewChild="getDepartments"
    />

  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import AddDept from './components/add-dept.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      showDialog: false,
      treeNode: {},
      departs: [],
      originalDeparts: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '大光头'
      }
    }
  },
  created() {
    this.getDepartments() // 调用自身的方法
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      // this.departs = result.depts // 需要将其转化成树形结构
      this.originalDeparts = result.depts
      this.departs = tranListToTreeData(result.depts, '')
      this.showDialog = false
      // console.log(result)
    },
    addDepts(treeNode) {
      this.showDialog = true
      // console.log(treeNode)
      this.treeNode = treeNode
    },
    editDepts(node) {
      console.log(node)
      this.showDialog = true
      // 当点击了 编辑按钮，我们再去获取部门信息
      // 怎么调用子组件上的方法
      // 通过ref获得 子组件实例对象 来调用
      this.$refs.addDeptRef.getDepartDetail(node.id)
    }
  }
}
</script>

 <style lang="scss" scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

